home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / C++ AppleLink Messages / CPlus.Dev$ 3⁄9⁄90 / 0064-Re CPLUS call by ref-Mar90 < prev    next >
Encoding:
Text File  |  1990-03-09  |  2.2 KB  |  85 lines  |  [TEXT/GEOL]

  1. Item forwarded  by  TIM.SWIHART  to GARDNER.P
  2.  
  3. Item    4466303                         6-March-90        20:27PST
  4.  
  5. From:   V0645                           Tandem, Gary Campbell,VAR
  6.  
  7. To:     MACDTS                          Macintosh Developer Tech. Supt.
  8.  
  9. cc:     CPLUS.DEV$                      C++ Interest List--Developers
  10.  
  11. Sub:    re:CPLUS call by ref type err
  12.  
  13. //
  14. // MPW 3.1
  15. // << CFront Version 1.0B1 (9/21/89; AT&T 2.0) ©Apple Computer, Inc. 1989 >>
  16.  
  17. // CPLUS test program that because of the type mismatch between the function
  18. // 'increment', and the argument passed to it, a temporary lvalue is generated,
  19. // but the contents of the temporary never get put into the variable 'y', and
  20. // thus the call to 'increment' is a do nothing.
  21. //
  22. // CPLUS marks the offending line with the following warning that is hard to
  23. // understand,
  24. //
  25. // # warning: conversion of lvalue needed: temporary used to initialize
  26. reference
  27. //
  28. // and then generates code that doesn't get the temporary 'lvalue'
  29. // back into ( converted some how ) the call by reference paramenter.
  30. //
  31. // I would be happier with a type mismatch error if possible, rather than
  32. debugging
  33. // a run time error.
  34. //
  35. // the code generation looks funny, after the call to increment the next usage
  36. // of 'y' is pulled out of a register, but 'y' isn't passed via a register
  37. // since it is call by reference.
  38. //
  39. // ZORTECH gets the same error, sans the warning message, so this must be
  40. // related to CFRONT.
  41.  
  42. //
  43. #include <stdio.h>
  44.  
  45. void increment(long &x)
  46. {
  47.    x++;
  48. }
  49. void test(long y)
  50. {
  51.    if (y!=4) {
  52.    printf("*** Error, y should be 4, instead y = %d\n",y);
  53.    } else {
  54.    printf("It worked, The value of y is %d\n",y);
  55.    }
  56. }
  57.  
  58. void main()
  59. {
  60.  
  61.    int y;
  62.    long yl;
  63.    //
  64.    // test program that passes an 'int' to a 'long &' call by reference, by
  65. mistake
  66.    // the warning message:
  67.    // # warning: conversion of lvalue needed: temporary used to initialize
  68. reference
  69.    // doesn't really tell you that the code being generated will not work
  70.    // and this should really be some kind of type checking error
  71.    //
  72.    yl = 3;
  73.    y = 3;
  74.    increment(y);
  75.    //
  76.    // 'y' should be 4 now, right!
  77.    // it would have been had 'y' been declared long.
  78.    //
  79.    test(y);
  80.    increment(yl);
  81.    test(yl);
  82.  
  83. }
  84.  
  85.